package org.javaboy.servlet_component_demo.config;

import org.javaboy.servlet_component_demo.filter.MyFilter;
import org.javaboy.servlet_component_demo.filter.MyFilter02;
import org.javaboy.servlet_component_demo.interceptor.MyInterceptor;
import org.javaboy.servlet_component_demo.servlet.MyServlet;
import org.javaboy.servlet_component_demo.servlet.MyServletContextListener;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;

@Configuration
@ImportResource("classpath:beans.xml")
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry
                //设置访问路径
                .addViewController("/my")
                .setViewName("hello");
//        registry.addViewController()
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/**");
    }

    @Bean
    ServletRegistrationBean myServlet01() {
        ServletRegistrationBean bean = new ServletRegistrationBean();
        bean.setServlet(new MyServlet());
        bean.setUrlMappings(Arrays.asList("/hello2"));
        return bean;
    }

    @Bean
    ServletListenerRegistrationBean myListener() {
        ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean();
        bean.setListener(new MyServletContextListener());
        return bean;
    }

    @Bean
    FilterRegistrationBean myFilter01() {
        FilterRegistrationBean<MyFilter> bean = new FilterRegistrationBean();
        bean.setFilter(new MyFilter());
        bean.setOrder(100);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }

    @Bean
    FilterRegistrationBean myFilter02() {
        FilterRegistrationBean<MyFilter02> bean = new FilterRegistrationBean();
        bean.setFilter(new MyFilter02());
        bean.setOrder(99);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }
}
